package org.jeecg.modules.unifysys.controller;

import java.util.Arrays;
import javax.servlet.http.HttpServletRequest;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.unifysys.entity.ApkVersion;
import org.jeecg.modules.unifysys.service.IApkVersionService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: apk包更新历史
 * @Author: jeecg-boot
 * @Date:   2021-04-20
 * @Version: V1.0
 */
@Api(tags="apk包更新历史")
@RestController
@RequestMapping("/unifysys/apkVersion")
@Slf4j
public class ApkVersionController extends JeecgController<ApkVersion, IApkVersionService> {
	@Autowired
	private IApkVersionService apkVsersionService;
	
	/**
	 * 分页列表查询
	 *
	 * @param apkVersion
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "apk包更新历史-分页列表查询")
	@ApiOperation(value="apk包更新历史-分页列表查询", notes="apk包更新历史-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ApkVersion apkVersion,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ApkVersion> queryWrapper = QueryGenerator.initQueryWrapper(apkVersion, req.getParameterMap());
		Page<ApkVersion> page = new Page<ApkVersion>(pageNo, pageSize);
		IPage<ApkVersion> pageList = apkVsersionService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param apkVersion
	 * @return
	 */
	@AutoLog(value = "apk包更新历史-添加")
	@ApiOperation(value="apk包更新历史-添加", notes="apk包更新历史-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ApkVersion apkVersion) {
		apkVersion.setId(null);
		apkVsersionService.save(apkVersion);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param apkVersion
	 * @return
	 */
	@AutoLog(value = "apk包更新历史-编辑")
	@ApiOperation(value="apk包更新历史-编辑", notes="apk包更新历史-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ApkVersion apkVersion) {
		apkVsersionService.updateById(apkVersion);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "apk包更新历史-通过id删除")
	@ApiOperation(value="apk包更新历史-通过id删除", notes="apk包更新历史-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		apkVsersionService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "apk包更新历史-批量删除")
	@ApiOperation(value="apk包更新历史-批量删除", notes="apk包更新历史-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.apkVsersionService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "apk包更新历史-通过id查询")
	@ApiOperation(value="apk包更新历史-通过id查询", notes="apk包更新历史-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ApkVersion apkVersion = apkVsersionService.getById(id);
		if(apkVersion==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(apkVersion);
	}

   /*
    * 导出excel
    *
    * @param request
    * @param apkVsersion
    *//*
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ApkVsersion apkVsersion) {
        return super.exportXls(request, apkVsersion, ApkVsersion.class, "apk包更新历史");
    }


      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    *//*
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ApkVsersion.class);
    }*/


	 @AutoLog(value = "apk检查更新")
	 @ApiOperation(value="检查是否最新的版本号", notes="检查更新")
	 @PostMapping(value = "/checkVersion")
	 public Result<?> checkVersion(@RequestBody ApkVersion apkVersion) {


//		 HashMap<String,String > map = new HashMap<String, String>();
		 Result<String>  result = new Result<>();

//		QueryWrapper<ApkVsersion> queryWrapper = new QueryWrapper<>();
//		ApkVsersion one = apkVsersionService.getOne(queryWrapper);
		 String version = apkVersion.getApkVersion();
		 QueryWrapper<ApkVersion> queryWrapper = new QueryWrapper<>();
		 queryWrapper.orderByDesc("create_time");
		 queryWrapper.last(" limit 1");
		 ApkVersion one = apkVsersionService.getOne(queryWrapper);
		 if(!version.equals(one.getApkVersion())){
			 result.setCode(500);
			 result.setMessage("检查到最新版，您的版本号："+version+",当前版本为："+one.getApkVersion());
			 result.setResult("http://120.78.158.120:10085/sys/common/static/apk/ytuniapp.apk");
			 return result;
		 }

		 return Result.OK("您的为最新版");

	 }

}
